package fragments;


import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.baseproject.BaseFragment;
import com.example.administrator.newxlmedical.R;
import com.irisian.irissdk.EnrollListener;
import com.irisian.irissdk.EnrollResult;
import com.irisian.irissdk.IrisSdk;
import com.irisian.irissdk.ReadDistanceCallBack;

import java.util.ArrayList;
import java.util.List;

import beans.RegisterInfoBean;
import constants.Constants;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.IrisUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseIrisRegisterFragment extends BaseFragment {
    public RegisterInfoBean bean;
    public List<EnrollResult> list = new ArrayList<>();

    public MyHandler allHandler;

    public BaseIrisRegisterFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initView() {
        super.initView();
        allHandler = SingletonClass.getSingleton(MyHandler.class);
        IrisUtil.initSdk(mContext);
        bean = (RegisterInfoBean) getArguments().getSerializable(Constants.BASE_IRIS_REGISTER_FRAGMENT);

        IrisSdk.openCamera(mContext);
        IrisSdk.startReadDistance(mContext, new ReadDistanceCallBack() {
            @Override
            public void onReadDistance(int i) {

            }
        });
        startEnroll();
    }

    private void startEnroll() {
        IrisSdk.startEnroll(new EnrollListener() {
            @Override
            public void onEnrollSuccess(EnrollResult enrollResult) {
                synchronized (list) {
                    list.add(enrollResult);
                    if (list.size() == 1) {
                        byte[] code = new byte[IrisSdk.getConfiguration().IRIS_TEMPLATE_LENGTH];
                        System.arraycopy(enrollResult.iris_code, 0, code, 0, IrisSdk.getConfiguration().IRIS_TEMPLATE_LENGTH);
                        String image = Base64.encodeToString(code, Base64.DEFAULT);
                        netWork(image);
//                        if (bean.isPut()) {
//                            updateUserInfo("set LeftImg = '" + image + "', RightImg = '" + image + "', UserType =1, RoleInfoId = " + bean.getRegisterRole() + " where UCode = '" + bean.getCode() + "'");
//                        } else {
//                            postUserInfo(image);
//                        }
                    }
                }
            }

            @Override
            public void onEnrollFailure(boolean b, int i, String s) {

            }
        }, 10000);
        IrisSdk.startReadDistance(mContext, new ReadDistanceCallBack() {
            @Override
            public void onReadDistance(int i) {

            }
        });
    }

    abstract void netWork(String image);

    @Override
    public void onDestroy() {
        super.onDestroy();
        IrisSdk.stopReadDistance(mContext);
        IrisSdk.closeCamera(mContext);
        IrisSdk.releaseSdk(mContext);

    }
}
